package com.picturestory.client.android.registration;

import org.json.JSONObject;

import com.picturestory.client.android.Dashboard;
import com.picturestory.client.android.R;
import com.picturestory.client.android.util.Constants;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfo extends Activity {
	
	private static final String TAG = UserInfo.class.getSimpleName();
	
	private static final int CHECK_DUPLICATE_USER_BY_ID_NUMBER = 1;
	private static final int CHECK_DUPLICATE_USER_BY_USER_ID = 4;
	private static final int CREATE_NEW_USER = 2;
	private static final int RETRIEVE_USER_BY_ID_NUMBER = 3;
	private static final int AUTHENTICATE = 5;
	
	private EditText mETUserId;
	private EditText mETPassword;
	private TextView mTVIdLabel;
	private TextView mTVPasswordLabel;
	private Button mNext;
	//
	private TextView mTestingResult;
	//
	
	private Bundle userInfo;
	private int which;
	private boolean validUserId;
	
	private Handler mLoginHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (validUserId) {
				startActivity(new Intent(UserInfo.this, Dashboard.class));
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Log.i(Constants.LOGTAG, TAG + ": on create");
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.user_info);
		
		mETUserId = (EditText) findViewById(R.id.user_info_id);
		mETPassword = (EditText) findViewById(R.id.user_info_password);
		mTVIdLabel = (TextView) findViewById(R.id.user_info_id_label);
		mTVPasswordLabel = (TextView) findViewById(R.id.user_info_password_label);
		mNext = (Button) findViewById(R.id.user_info_next);
		//
		mTestingResult = (TextView) findViewById(R.id.testing_result);
		//
		
		
		Typeface font = Typeface.createFromAsset(getAssets(), "perpetua.ttf");
		
		mTVIdLabel.setTypeface(font);
		mTVPasswordLabel.setTypeface(font);
		mNext.setTypeface(font);
		
		mNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				which = CHECK_DUPLICATE_USER_BY_USER_ID;
				Intent newIntent = new Intent(UserInfo.this, UserHttpRequester.class);
				newIntent.putExtra("userId", mETUserId.getText().toString());
				newIntent.putExtra("which", which);
				startActivityForResult(newIntent, 1);
			}
			
		});

		Intent intent = UserInfo.this.getIntent();
		userInfo = intent.getExtras();
		validUserId = false;
		
		which = CHECK_DUPLICATE_USER_BY_ID_NUMBER;
		Intent newIntent = new Intent(UserInfo.this, UserHttpRequester.class);
		newIntent.putExtra("idNumber", userInfo.getString("idNumber"));
		newIntent.putExtra("which", which);
		startActivityForResult(newIntent, 1);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		//Toast.makeText(this, "return from ActivityForResult", Toast.LENGTH_LONG).show();
		
		Log.i(Constants.LOGTAG, TAG + " : onActivityResult with data=" + data.getStringExtra("result"));
		
		String jsonString = data.getStringExtra("result");
		mTestingResult.setText(jsonString);
		//mTestingResult.invalidate();
		
		JSONObject json = null;
		try {
			json = new JSONObject(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		switch(which) {
		case CHECK_DUPLICATE_USER_BY_ID_NUMBER:
			try {
				if (json.getString("message").equals("DUPLICATE")) {
					which = RETRIEVE_USER_BY_ID_NUMBER;
					Intent newIntent = new Intent(UserInfo.this, UserHttpRequester.class);
					newIntent.putExtra("idNumber", userInfo.getString("idNumber"));
					newIntent.putExtra("which", which);
					startActivityForResult(newIntent, 1);
					
					validUserId = true;
					
					Toast.makeText(this, "Please do not change User ID! I'm going to protect this by locking the field :P", Toast.LENGTH_LONG).show();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case CHECK_DUPLICATE_USER_BY_USER_ID:
			try {
				if (validUserId == false && json.getString("message").equals("DUPLICATE")) {
					Toast.makeText(UserInfo.this, "Duplicate User Id", Toast.LENGTH_LONG).show();
				} else {
					validUserId = true;
				}
				new Thread() {
					@Override
					public void run() {
						mLoginHandler.handleMessage(mLoginHandler.obtainMessage());
					}
				}.start();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case CREATE_NEW_USER:
			break;
		case RETRIEVE_USER_BY_ID_NUMBER:
			try {
				mETUserId.setText(json.getString("userId"));
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		}
	}
	
	private JSONObject getJsonObject(String in) {
		JSONObject json = null;
		try {
			json = new JSONObject(in);
		} catch (Exception e) {
			return null;
		}
		return json;
	}

}
